package com.book.oauth.server.user.hystrix;

import com.book.common.api.entity.CurrentUser;
import com.book.common.api.oauth.HystrixConstant;
import com.book.common.result.RestResponse;
import com.book.oauth.server.user.dto.UserDto;
import com.book.oauth.server.user.hystrix.impl.OauthHystrixImpl;
import com.book.oauth.server.user.vo.CurrentUserDetail;
import com.book.oauth.server.user.vo.UserVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Description：用户TOKEN信息接口,执行的filter顺序 <p>
 * DefaultSecurityFilterChain(获取Filter顺序)
 * <p>
 * Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/oauth/token'], Ant [pattern='/oauth/token_key'], Ant [pattern='/oauth/check_token']]],
 * WebAsyncManagerIntegrationFilter,SecurityContextPersistenceFilter,HeaderWriterFilter,
 * LogoutFilter,ClientCredentialsTokenEndpointFilter,BasicAuthenticationFilter,
 * RequestCacheAwareFilter,SecurityContextHolderAwareRequestFilter,AnonymousAuthenticationFilter,
 * SessionManagementFilter,ExceptionTranslationFilter,FilterSecurityInterceptor,
 * <p>
 * Creating filter chain: org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration$NotOAuthRequestMatcher,
 * WebAsyncManagerIntegrationFilter,SecurityContextPersistenceFilter,HeaderWriterFilter,
 * LogoutFilter,OAuth2AuthenticationProcessingFilter,RequestCacheAwareFilter,
 * SecurityContextHolderAwareRequestFilter,AnonymousAuthenticationFilter,SessionManagementFilter,
 * ExceptionTranslationFilter,FilterSecurityInterceptor,
 * <p>
 * Creating filter chain: any request,
 * WebAsyncManagerIntegrationFilter,SecurityContextPersistenceFilter,HeaderWriterFilter,
 * LogoutFilter,RequestCacheAwareFilter,SecurityContextHolderAwareRequestFilter
 * AnonymousAuthenticationFilter,SessionManagementFilter,ExceptionTranslationFilter,
 * FilterSecurityInterceptor,
 * POST对象用@RequestBody
 * GET对象用@SpringQueryMap
 * <p>
 *
 * @Author： leo.xiong
 * @CreateDate： 2019/10/14 15:11
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@FeignClient(value = "oauth-server", fallbackFactory = OauthHystrixImpl.class)
public interface IOauthHystrix {
    /**
     * 获取token
     * TokenEndpoint获取token信息
     * SecurityContextPersistenceFilter,ClientCredentialsTokenEndpointFilter,OAuth2AuthenticationProcessingFilter默认过滤器
     * 这个如果配置支持allowFormAuthenticationForClients的，且url中有client_id和client_secret的会走ClientCredentialsTokenEndpointFilter来保护
     * 如果没有支持allowFormAuthenticationForClients或者有支持但是url中没有client_id和client_secret的，走basic认证保护
     *
     * @param username
     * @param password
     * @param client_id
     * @param client_secret
     * @param grant_type
     * @param scope
     * @return
     */
    @PostMapping(value = "/oauth/token")
    Map<String, String> getOauthToken(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("client_id") String client_id, @RequestParam("client_secret") String client_secret, @RequestParam("grant_type") String grant_type, @RequestParam("scope") String scope);

    /**
     * 获取当前token的Principal对象信息
     *
     * @param token
     * @return
     */
    @PostMapping(value = "/server/principal")
    RestResponse<CurrentUser> getPrincipal(@RequestParam(HystrixConstant.BOOK_TOKEN) String token);

    /**
     * 根据当前用户信息，获取当前用户详情信息
     *
     * @param currentUser
     * @return
     */
    @PostMapping(value = "/server/currentUserDetail")
    RestResponse<CurrentUserDetail> getCurrentUserDetail(@RequestBody CurrentUser currentUser);

    /**
     * 清除token（注销登录）
     *
     * @param token
     * @return
     */
    @GetMapping(value = "/server/logout")
    RestResponse<String> logout(@RequestParam(HystrixConstant.BOOK_TOKEN) String token);

    /**
     * 注册
     *
     * @param userDto
     * @return
     */
    @PostMapping(value = "/server/registered")
    RestResponse<UserVo> registered(@RequestBody UserDto userDto);
}
